package top.iridescent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.iridescent.db.annotation.DataSource;
import top.iridescent.dao.model.request.MhDecorationsInfoQuery;
import top.iridescent.entity.MhDecorationsInfo;
import top.iridescent.service.impl.MhDecorationsInfoServiceImpl;
import top.iridescent.util.ObjectUtils;
import top.iridescent.util.PageUtils;
import top.iridescent.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @email
 * @date 2021-09-17 15:17:44
 */
@RestController
@Api(tags = "MhDecorationsInfo")
@RequestMapping("mh/mhdecorationsinfo")
@CrossOrigin
//@DataSource("mh_game_local")
public class MhDecorationsInfoController {
    @Autowired
    private MhDecorationsInfoServiceImpl service;

    /**
     * 列表
     */
    @ApiOperation(value = "分页检索", notes = "")
    @PostMapping("/list")
    //@RequiresPermissions("mh:mhdecorationsinfo:list")
    public Result findPage(@RequestBody MhDecorationsInfoQuery param) {
        return service.findPage(PageUtils.getPage(param), param);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "单个检索 ", notes = "")
    @GetMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("mh:mhdecorationsinfo:info")
    public Result getById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        return service.getById(objId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增", notes = "")
    @PostMapping(value = "", produces = "application/json")
    //@RequiresPermissions("mh:mhdecorationsinfo:save")
    public Result save(@RequestBody MhDecorationsInfo param) {
        //param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.saveInfo(param);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新", notes = "")
    @PutMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("mh:mhdecorationsinfo:update")
    public Result update(@ApiParam(name = "objId", value = "") @PathVariable Long objId, @RequestBody MhDecorationsInfo param) {
        param.setId(objId);
        //param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.update(param);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("mh:mhdecorationsinfo:delete")
    public Result deleteById(@ApiParam(name = "objId", value = "") @PathVariable Long objId) {
        return service.deleteById(objId);
    }


    /**
     * 获取可用
     */
    @ApiOperation(value = "获取", notes = "")
    @GetMapping(value = "/all/{objId}", produces = "application/json")
    public Result getLS(@ApiParam(name = "objId", value = "0女1男") @PathVariable Long objId) {
        return service.getNoUsedEQ(objId);
    }

    /**
     * 获取统计
     */
    @ApiOperation(value = "获取", notes = "")
    @GetMapping(value = "/statistic", produces = "application/json")
    public Result statistic() {
        QueryWrapper<MhDecorationsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(price) as price,sum(sale_price) as sale_price");
        MhDecorationsInfo a1 = service.getOne(queryWrapper);
        queryWrapper.lambda().eq(MhDecorationsInfo::getStatus, "0");
        MhDecorationsInfo a2 = service.getOne(queryWrapper);
        Map map = new HashMap();
        if (ObjectUtils.isEmpty(a1)) {
            map.put("total", 0);
            map.put("recover", 0);
            map.put("loss", 0);
        } else {
            map.put("total", a1.getPrice());
            map.put("recover", a1.getSalePrice());
            map.put("loss", a1.getSalePrice().subtract(a1.getPrice()));
        }

        if (ObjectUtils.isEmpty(a2)) {
            map.put("recovered", 0);
        } else {
            map.put("recovered", a2.getSalePrice());
        }

        return Result.ok().setData(map);
    }
}
